/*
 * MainFrame.java
 *
 * Created on June 25, 2007, 11:42 AM
 */

package net.sverdianto.regeat;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 *
 * @author  sverdianto
 */
public class MainFrame extends javax.swing.JFrame {
    
    Pattern pattern;
    Matcher matcher;
    
    /** Creates new form MainFrame */
    public MainFrame() {
        initComponents();
    }
    
    /** 
     * This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jLabelPattern = new javax.swing.JLabel();
        jTextFieldPattern = new javax.swing.JTextField();
        jTabbedPaneResult = new javax.swing.JTabbedPane();
        jScrollPaneHighlighted = new javax.swing.JScrollPane();
        jEditorPaneHighlighted = new javax.swing.JEditorPane();
        jCheckBoxValid = new javax.swing.JCheckBox();
        jLabelText = new javax.swing.JLabel();
        jScrollPaneText = new javax.swing.JScrollPane();
        jTextAreaText = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("RegEat - Eat Regular Expression");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabelPattern.setText("Pattern");

        jTextFieldPattern.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextFieldPatternKeyReleased(evt);
            }
        });

        jEditorPaneHighlighted.setEditable(false);
        jEditorPaneHighlighted.setContentType("text/html");
        jEditorPaneHighlighted.setDragEnabled(false);
        jScrollPaneHighlighted.setViewportView(jEditorPaneHighlighted);

        jTabbedPaneResult.addTab("Matches", jScrollPaneHighlighted);

        jCheckBoxValid.setSelected(true);
        jCheckBoxValid.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        jCheckBoxValid.setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/net/sverdianto/regeat/resources/unchecked.gif")));
        jCheckBoxValid.setDisabledSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/net/sverdianto/regeat/resources/checked.gif")));
        jCheckBoxValid.setEnabled(false);
        jCheckBoxValid.setIcon(new javax.swing.ImageIcon(getClass().getResource("/net/sverdianto/regeat/resources/checked.gif")));
        jCheckBoxValid.setMargin(new java.awt.Insets(0, 0, 0, 0));
        jCheckBoxValid.setSelectedIcon(new javax.swing.ImageIcon(getClass().getResource("/net/sverdianto/regeat/resources/unchecked.gif")));

        jLabelText.setText("Text");

        jTextAreaText.setColumns(20);
        jTextAreaText.setRows(5);
        jTextAreaText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextAreaTextKeyReleased(evt);
            }
        });

        jScrollPaneText.setViewportView(jTextAreaText);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabelText)
                    .add(jLabelPattern))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jScrollPaneText, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE)
                    .add(jTextFieldPattern, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 389, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jCheckBoxValid)
                .add(19, 19, 19))
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jTabbedPaneResult, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabelPattern)
                    .add(jTextFieldPattern, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jCheckBoxValid))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabelText)
                    .add(jScrollPaneText, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 58, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(3, 3, 3)
                .add(jTabbedPaneResult, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        System.gc();
    }//GEN-LAST:event_formWindowClosing

    private void jTextAreaTextKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextAreaTextKeyReleased
        check();
    }//GEN-LAST:event_jTextAreaTextKeyReleased

    private void jTextFieldPatternKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldPatternKeyReleased
        check();
    }//GEN-LAST:event_jTextFieldPatternKeyReleased
    
    /**
     * This method is called to perform pattern matching, doing all its jobs to
     * display highligted matched string and debuging info
     */
    private void check() {
        try {
            // get data from form
            String patternText = jTextFieldPattern.getText();
            String targetText = jTextAreaText.getText();
            
            // compile typed pattern & string to test
            pattern = Pattern.compile(patternText);
            matcher = pattern.matcher(targetText);
            
            // highlighted text
            StringBuffer highlighted = new StringBuffer();
            
            highlighted.append("<font face='Arial' size='20'>");
            int end = 0;
            while (matcher.find()) {
                String replacement = "<b color='#0066ff'>" + matcher.group() + "</b>";
                matcher.appendReplacement(highlighted, replacement);
                end = matcher.end();
            }
            if(end == 0) {
                highlighted.append(targetText);
            }
            else {
                highlighted.append(targetText.substring(end));
            }
            highlighted.append("</font>");
            
            String resultText = highlighted.toString();
            resultText = resultText.replaceAll("\n", "<br>");
            
            // display highlighted text
            jEditorPaneHighlighted.setText(resultText.toString());
            
            jCheckBoxValid.setSelected(true);
        } catch(PatternSyntaxException pse) {
            // Pattern is not valid
            jCheckBoxValid.setSelected(false);
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox jCheckBoxValid;
    private javax.swing.JEditorPane jEditorPaneHighlighted;
    private javax.swing.JLabel jLabelPattern;
    private javax.swing.JLabel jLabelText;
    private javax.swing.JScrollPane jScrollPaneHighlighted;
    private javax.swing.JScrollPane jScrollPaneText;
    private javax.swing.JTabbedPane jTabbedPaneResult;
    private javax.swing.JTextArea jTextAreaText;
    private javax.swing.JTextField jTextFieldPattern;
    // End of variables declaration//GEN-END:variables
    
}
